package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.HomeworkJdbc;
import com.wuseguang.springcloud.dao.mapper.HomeworkMapper;
import com.wuseguang.springcloud.dao.repository.HomeworkRepository;
import com.wuseguang.springcloud.entity.Homework;
import com.wuseguang.springcloud.service.HomeworkService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.PaginationUtil;
import com.wuseguang.springcloud.util.PagingTool;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 作业表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-01-16
 */
@Service
public class HomeworkServiceImpl implements HomeworkService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private HomeworkRepository homeworkRepository;
    @Autowired
    private HomeworkMapper homeworkMapper;
    @Autowired
    private HomeworkJdbc homeworkJdbc;

    @Override
    public Result findHomework(String[] classId, Integer indexNo, String teacherId,Integer rows) {

        logger.info("查询班级作业 HomeworkServiceImpl -> findHomework");
        //转为String 类型
        String classid = org.apache.commons.lang.StringUtils.join(classId,",");

        //查询班级作业sql
        String sql = "select h.id,h.name,h.content,h.picture_url,video_url,link,publisher,teacher_id,c.name classname from homework h " +
                "left join (select c.id,CONCAT(c.name,g.name) name from classes c left JOIN grade g on(g.id=c.grade_id) ) c on(h.class_id=c.id)  " +
                "where   h.class_id in("+classid+") and h.teacher_id = ?  ORDER BY h.id desc";

        //作业list
        List<Map<String,Object >> list = null;

        try{
            list = homeworkJdbc.queryListMap(sql,teacherId);
        }catch (Exception e){
            logger.info("无作业信息");
        }

        //创建分页工具
        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(list);
        //返回数据
        return new Result(new PaginationUtil<>(list.size(),rows, tool.getList(), indexNo));
    }

    @Override
    public Result addHomework(Homework homework, String teacherId, String teacherName,String termId) {

        logger.info("添加班级作业 HomeworkServiceImpl -> addHomework");

        //发布人名称
        homework.setPublisher(teacherName);
        //教师id
        homework.setTeacherId(Integer.valueOf(teacherId));
        //来源
        homework.setSource(1);
        //学期
        homework.setTermId(Integer.valueOf(termId));

        homeworkRepository.save(homework);

        return new Result();
    }

    @Override
    public Result deleteHomework(Integer id,String teacherId) {

        //查询教师id
        Integer tId = homeworkRepository.findById(id).get().getTeacherId();
        //判断是否相同
        if(String.valueOf(tId).equals(teacherId)){
            return  new Result(CodeUtil.NO_PUBLISHER_NO_CAO_ZU);
        }


        logger.info("删除作业 HomeworkServiceImpl -> deleteHomework");
        homeworkRepository.deleteById(id);

        return new Result();
    }
}
